﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using System.Web.Configuration;
using MySql.Data.MySqlClient;
using System.Data;

namespace ES_Project.Pages
{
    public partial class WebForm14 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getData();
                emp_id.Text = Request.QueryString["emp_id"];
                if (!emp_id.Text.Equals(""))
                {
                    emp_id.Text = Request.QueryString["emp_id"];
                    CMD_INSERT.Enabled = false;
                    CMD_EDIT.Enabled = true;
                    Editing();
                }
                else
                {
                    CMD_INSERT.Enabled = true;
                    CMD_EDIT.Enabled = false;
                }
            }
        }

        void Editing()
        {
            CMD_INSERT.Enabled = false;
            CMD_EDIT.Enabled = true;
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;




            strSQL = "select emp_id, emp_code, emp_nickname, emp_degree, emp_institution, emp_degree_subject, "+
                     "emp_birthday, emp_age, emp_gender, emp_id_card, emp_address, emp_phone, emp_account_no, "+
                     "emp_bank_name, emp_bank_branch, emp_tax_no, emp_tax_invoice, emp_email, emp_begin, emp_end, "+ 
                     "emp_quit, pay_id, emp_name, emp_position, emp_type "+
                     "from emp_mast em where em.emp_id = '"+emp_id.Text+"'";

            Conn.Open();
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds4 = new DataSet();
            da4.Fill(ds4);

            int _row = ds4.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i)
            {

                emp_id.Text = Convert.ToString(ds4.Tables[0].Rows[i]["emp_id"]).ToString();
                empCode.Text = (string)ds4.Tables[0].Rows[i]["emp_code"].ToString();
                empName.Text = (string)ds4.Tables[0].Rows[i]["emp_name"].ToString();
                empNickname.Text = (string)ds4.Tables[0].Rows[i]["emp_nickname"].ToString();
                empDrgree.Text = (string)ds4.Tables[0].Rows[i]["emp_degree"].ToString();
                empInstitution.Text = (string)ds4.Tables[0].Rows[i]["emp_institution"].ToString();
                empDegreeSubject.Text = (string)ds4.Tables[0].Rows[i]["emp_degree_subject"].ToString();
                empBirthday.Text = (string)ds4.Tables[0].Rows[i]["emp_birthday"].ToString();
                empAge.Text = (string)ds4.Tables[0].Rows[i]["emp_age"].ToString();
                empGender.Text = (string)ds4.Tables[0].Rows[i]["emp_gender"].ToString();
                empIdCard.Text = (string)ds4.Tables[0].Rows[i]["emp_id_card"].ToString();
                empAddress.Text = (string)ds4.Tables[0].Rows[i]["emp_address"].ToString();
                empPhone.Text = (string)ds4.Tables[0].Rows[i]["emp_phone"].ToString();
                empAccountNo.Text = (string)ds4.Tables[0].Rows[i]["emp_account_no"].ToString();
                empBankName.Text = (string)ds4.Tables[0].Rows[i]["emp_bank_name"].ToString();
                empBankBranch.Text = (string)ds4.Tables[0].Rows[i]["emp_bank_branch"].ToString();
                empTaxNo.Text = (string)ds4.Tables[0].Rows[i]["emp_tax_no"].ToString();
                empTaxInvoice.Text = (string)ds4.Tables[0].Rows[i]["emp_tax_invoice"].ToString();
                empEmail.Text = (string)ds4.Tables[0].Rows[i]["emp_email"].ToString();
                empBegin.Text = (string)ds4.Tables[0].Rows[i]["emp_begin"].ToString();
                empEnd.Text = (string)ds4.Tables[0].Rows[i]["emp_end"].ToString();
                empQuit.Text = (string)ds4.Tables[0].Rows[i]["emp_quit"].ToString();
                payId.Text = Convert.ToString(ds4.Tables[0].Rows[i]["pay_id"]).ToString();
                empPosition.Text = Convert.ToString(ds4.Tables[0].Rows[i]["emp_position"]).ToString();
                empType.SelectedValue = Convert.ToString(ds4.Tables[0].Rows[i]["emp_type"]).ToString();

            }
        }
        void getData()
        {

            MySqlConnection cnx = new MySqlConnection(StrConn);
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            string cmdText = "SELECT * FROM emp_mast";
            MySqlCommand cmd = new MySqlCommand(cmdText, cnx);
            DataSet ds = new DataSet();
            adapter.SelectCommand = cmd;
            adapter.Fill(ds);
            GridView4.DataSource = ds;
            GridView4.DataBind();
        }
        protected void CMD_INSERT_Click(object sender, EventArgs e)
        {
             using (essystemEntities context = new essystemEntities())
             {
                Button button = (Button)sender;
                switch (button.ID)
                {
                    case "CMD_INSERT":
                    List<emp_mast> sm = context.emp_mast.Where("it.[emp_code]='" + empCode.Text + "' ").ToList();
                    if (sm.Count == 0)
                    {
                        emp_mast empMast = new emp_mast();
                        empMast.emp_code = empCode.Text;
                        empMast.emp_name = empName.Text;
                        empMast.emp_nickname = empNickname.Text;
                        empMast.emp_degree = empDrgree.Text;
                        empMast.emp_degree_subject = empDegreeSubject.Text;
                        empMast.emp_institution = empInstitution.Text;
                        if (empBirthday.Text != null && empBirthday.Text != "")
                        {
                            empMast.emp_birthday = Convert.ToDateTime(empBirthday.Text);
                        }
                        if (empAge.Text != null && empAge.Text != "")
                        {
                            empMast.emp_age = Convert.ToInt32(empAge.Text);
                        }
                        empMast.emp_gender = empGender.Text;
                        empMast.emp_id_card = empIdCard.Text;
                        empMast.emp_address = empAddress.Text;
                        empMast.emp_phone = empPhone.Text;
                        empMast.emp_account_no = empAccountNo.Text;
                        empMast.emp_bank_name = empBankName.Text;
                        empMast.emp_bank_branch = empBankBranch.Text;
                        empMast.emp_tax_no = empTaxNo.Text;
                        empMast.emp_tax_invoice = empTaxInvoice.Text;
                        empMast.emp_email = empEmail.Text;
                        if (empBegin.Text != null && empBegin.Text != "")
                        {
                            empMast.emp_begin = Convert.ToDateTime(empBegin.Text);
                        }
                        if (empEnd.Text != null && empEnd.Text != "")
                        {
                            empMast.emp_end = Convert.ToDateTime(empEnd.Text);
                        }
                        empMast.emp_quit = empQuit.Text;
                        empMast.create_by = "Tong";
                        empMast.create_date = DateTime.Now;
                        empMast.emp_position = empPosition.Text;
                        empMast.emp_type = empType.SelectedValue;
                        context.emp_mast.AddObject(empMast);
                        if (context.SaveChanges() > 0)
                        {

                            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert1", "alert('บันทึกเรียบร้อยแล้ว')", true);
                            GridView4.DataBind();
                            GridView4.SelectedIndex = -1;
                            clear();
                        }
                        
                    }
                    else 
                    { 
                        ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ReturnMessini9", "alert('รหัสวิชาเรียนซ้ำ');", true);
                    }
                    break;

                    case "CMD_EDIT":
                         List<emp_mast> empMS = context.emp_mast.Where("it.[emp_id]=" + emp_id.Text + " ").ToList();
                         if (empMS.Count() == 1)
                         {
                             emp_mast empMEdit = context.emp_mast.Where("it.[emp_id]=" + emp_id.Text + "").First();
                             if (empMEdit != null) {
                                 empMEdit.emp_code = empCode.Text;
                                 empMEdit.emp_name = empName.Text;
                                 empMEdit.emp_nickname = empNickname.Text;
                                 empMEdit.emp_degree = empDrgree.Text;
                                 empMEdit.emp_degree_subject = empDegreeSubject.Text;
                                 empMEdit.emp_institution = empInstitution.Text;
                                 empMEdit.update_by = "Tong";
                                 empMEdit.update_date = DateTime.Now;
                               

                                 if (empBirthday.Text != null && empBirthday.Text != "")
                                 {
                                     empMEdit.emp_birthday = Convert.ToDateTime(empBirthday.Text);
                                 }
                                 if (empAge.Text != null && empAge.Text != "")
                                 {
                                     empMEdit.emp_age = Convert.ToInt32(empAge.Text);
                                 }

                                
                                 empMEdit.emp_gender = empGender.Text;
                                 empMEdit.emp_id_card = empIdCard.Text;
                                 empMEdit.emp_address = empAddress.Text;
                                 empMEdit.emp_phone = empPhone.Text;
                                 empMEdit.emp_account_no = empAccountNo.Text;
                                 empMEdit.emp_bank_name = empBankName.Text;
                                 empMEdit.emp_bank_branch = empBankBranch.Text;
                                 empMEdit.emp_tax_no = empTaxNo.Text;
                                 empMEdit.emp_tax_invoice = empTaxInvoice.Text;
                                 empMEdit.emp_email = empEmail.Text;
                                 empMEdit.emp_begin = Convert.ToDateTime(empBegin.Text);
                                 if (empEnd.Text != null && empEnd.Text != "")
                                 {
                                     empMEdit.emp_end = Convert.ToDateTime(empEnd.Text);
                                 }
                               
                                 empMEdit.emp_quit = empQuit.Text;
                                 empMEdit.emp_position = empPosition.Text;
                                 empMEdit.emp_type = empType.SelectedValue;

                                 if (context.SaveChanges() > 0)
                                 {

                                     ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert2", "alert('แก้ไขข้อมูลเรียบร้อยแล้ว')", true);
                                     GridView4.DataBind();
                                     GridView4.SelectedIndex = -1;
                                     CMD_INSERT.Enabled = true;
                                     CMD_EDIT.Enabled = false;
                                     clear();

                                 }
                             }
                         }
                        break;
                }

            }
             getData(); 
        }
        protected void GridView4_SelectedIndexChanged(object sender, EventArgs e)
        { 
            CMD_INSERT.Enabled = false;
            CMD_EDIT.Enabled = true;

            using (essystemEntities context = new essystemEntities())
            {

                
                    List<emp_mast> mb = context.emp_mast.Where("it.[emp_id]=" + ((HiddenField)GridView4.SelectedRow.Cells[1].FindControl("HiddenField3")).Value.ToString() + "").ToList();
                    if (mb.Count > 0)
                    {
                        empCode.Text = mb[0].emp_code;
                        empName.Text = mb[0].emp_name;
                        empNickname.Text = mb[0].emp_nickname;
                        empDrgree.Text = mb[0].emp_gender;
                        emp_id.Text = mb[0].emp_id.ToString();
                        empDegreeSubject.Text = mb[0].emp_degree_subject;
                        empInstitution.Text = mb[0].emp_institution;
                        empBirthday.Text = Convert.ToString(mb[0].emp_birthday);
                        empAge.Text = Convert.ToString(mb[0].emp_age);
                        empGender.Text = mb[0].emp_gender;
                        empIdCard.Text = mb[0].emp_id_card;
                        empAddress.Text = mb[0].emp_address;
                        empPhone.Text = mb[0].emp_phone;
                        empAccountNo.Text = mb[0].emp_account_no;
                        empBankName.Text = mb[0].emp_bank_name;
                        empBankBranch.Text = mb[0].emp_bank_branch;
                        empTaxNo.Text = mb[0].emp_tax_no;
                        empTaxInvoice.Text = mb[0].emp_tax_invoice;
                        empEmail.Text = mb[0].emp_email;
                        empBegin.Text = Convert.ToString(mb[0].emp_begin);
                        empEnd.Text = Convert.ToString(mb[0].emp_end);
                        empQuit.Text = mb[0].emp_quit;
                        empType.SelectedValue = mb[0].emp_type;
                        empPosition.Text = mb[0].emp_position;
                        
                    }
                
                 
            }

        }

        protected void GridView4_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

            CMD_INSERT.Enabled = false;
            CMD_EDIT.Enabled = true;
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;




            strSQL = "select emp_id, emp_code, emp_nickname, emp_degree, emp_institution, emp_degree_subject, " +
                     "emp_birthday, emp_age, emp_gender, emp_id_card, emp_address, emp_phone, emp_account_no, " +
                     "emp_bank_name, emp_bank_branch, emp_tax_no, emp_tax_invoice, emp_email, emp_begin, emp_end, " +
                     "emp_quit, pay_id, emp_name " +
                     "from emp_mast em where em.emp_id = '" + GridView4.DataKeys[e.NewSelectedIndex].Value + "'";

            Conn.Open();
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds4 = new DataSet();
            da4.Fill(ds4);

            int _row = ds4.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i)
            {

                //clsRegDetailId.Text = Convert.ToString(ds4.Tables[0].Rows[i]["cls_reg_detail_id"]).ToString();
                //clsRegCode.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_code"].ToString();
                //clsName.Text = (string)ds4.Tables[0].Rows[i]["cls_name"].ToString();
                //clsCourseTime.Text = (string)ds4.Tables[0].Rows[i]["cls_course_time"].ToString();
                //clsCourseAmt.Text = (string)ds4.Tables[0].Rows[i]["cls_course_amt"].ToString();
                //clsRegId.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_id"].ToString();
                //clsCourseNo.Text = (string)ds4.Tables[0].Rows[i]["cls_course_no"].ToString();
                empCode.Text = (string)ds4.Tables[0].Rows[i]["emp_code"].ToString();
                empName.Text = (string)ds4.Tables[0].Rows[i]["emp_name"].ToString();
                empNickname.Text = (string)ds4.Tables[0].Rows[i]["emp_nickname"].ToString();
                empDrgree.Text = (string)ds4.Tables[0].Rows[i]["emp_degree"].ToString();
                emp_id.Text = Convert.ToString(ds4.Tables[0].Rows[i]["emp_id"]).ToString();
                empDegreeSubject.Text = (string)ds4.Tables[0].Rows[i]["emp_degree_subject"].ToString();
                empInstitution.Text = (string)ds4.Tables[0].Rows[i]["emp_institution"].ToString();
                empBirthday.Text = (string)ds4.Tables[0].Rows[i]["emp_birthday"].ToString();
                empAge.Text = (string)ds4.Tables[0].Rows[i]["emp_age"].ToString();
                empGender.Text = (string)ds4.Tables[0].Rows[i]["emp_gender"].ToString();
                empIdCard.Text = (string)ds4.Tables[0].Rows[i]["emp_id_card"].ToString();
                empAddress.Text = (string)ds4.Tables[0].Rows[i]["emp_address"].ToString();
                empPhone.Text = (string)ds4.Tables[0].Rows[i]["emp_phone"].ToString();
                empAccountNo.Text = (string)ds4.Tables[0].Rows[i]["emp_account_no"].ToString();
                empBankName.Text = (string)ds4.Tables[0].Rows[i]["emp_bank_name"].ToString();
                empBankBranch.Text = (string)ds4.Tables[0].Rows[i]["emp_bank_branch"].ToString();
                empTaxNo.Text = (string)ds4.Tables[0].Rows[i]["emp_tax_no"].ToString();
                empTaxInvoice.Text = (string)ds4.Tables[0].Rows[i]["emp_tax_invoice"].ToString();
                empEmail.Text = (string)ds4.Tables[0].Rows[i]["emp_email"].ToString();
                empBegin.Text = (string)ds4.Tables[0].Rows[i]["emp_begin"].ToString();
                empEnd.Text = (string)ds4.Tables[0].Rows[i]["emp_end"].ToString();
                empQuit.Text = (string)ds4.Tables[0].Rows[i]["emp_quit"].ToString();
                empPosition.Text = Convert.ToString(ds4.Tables[0].Rows[i]["emp_position"]).ToString();
                empType.SelectedValue = Convert.ToString(ds4.Tables[0].Rows[i]["emp_type"]).ToString();

            }
        }
        protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{

            //    ImageButton ib = (ImageButton)e.Row.FindControl("ImageButton1");
            //    ib.Attributes.Add("onclick", "return confirm('คุณต้องการที่จะลบข้อมูล?');");

            //}
            HyperLink HyFO4 = (HyperLink)(e.Row.FindControl("HyFO4"));
            if (HyFO4 != null)
            {
                // HyFO4.Text = (string)DataBinder.Eval(e.Row.DataItem, "AgentCode");
                //HyFO4.NavigateUrl = "Pay_Course.aspx?emp_id=" + (string)DataBinder.Eval(e.Row.DataItem, "emp_id").ToString();
                HyFO4.NavigateUrl = "#";
                HyFO4.Attributes.Add("Onclick", "opennewsletter('Pay_Course.aspx?emp_id=" + (string)DataBinder.Eval(e.Row.DataItem, "emp_id").ToString() + "');");
                //+ "&TxtAO=" + TxtAO.Text + "&TxtAgent=" + TxtAgent.Text + "&TxtInvoice1=" + TxtInvoice1.Text + "&TxtInvoice2=" + TxtInvoice2.Text + "&TxtDate1=" + TxtDate1.Text + "&TxtDate2=" + TxtDate2.Text + "&StatusWork=" + StatusWork.SelectedValue;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
                    }
                }
            }
        }
        //private void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    //*** hplHyperLink ***'
        //    HyperLink hplHyperLink = (HyperLink)e.Row.FindControl("HyFO4");
        //    if ((hplHyperLink != null))
        //    {
        //        hplHyperLink.NavigateUrl = "#";
        //        hplHyperLink.Attributes.Add("Onclick", "javascript::openW('Pay_Course.aspx?emp_id=" + (string)DataBinder.Eval(e.Row.DataItem, "emp_id").ToString() + "');");
        //    }
        //}
        private void clear()
        {
            empCode.Text = "";
            empName.Text = "";
            empNickname.Text = "";
            empDrgree.Text = "";
            empName.Text = "";
            empBegin.Text = "";
            empEnd.Text = "";
            empBirthday.Text = "";
            empAccountNo.Text = "";
            empAddress.Text = "";
            empAge.Text = "";
            empBankBranch.Text = "";
            empBankName.Text = "";
            empDegreeSubject.Text = "";
            empEmail.Text = "";
            empIdCard.Text = "";
            empPhone.Text = "";
            empGender.Text = "";
            empInstitution.Text = "";
            empTaxInvoice.Text = "";
            empTaxNo.Text = "";
            empQuit.Text = "";
            empPosition.Text = "";
        }


        protected void CMD_CLEAR_Click(object sender, EventArgs e)
        {
            clear();
            CMD_INSERT.Enabled = false;
            CMD_EDIT.Enabled = true;
        }

        protected void GridView4_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM emp_mast WHERE emp_id ='" + GridView4.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();



            GridView4.EditIndex = -1;
            getData();
        }

    }
}